package com.taotao.antlr4.calcu;

public class MyCalculatorVisitor extends CalculatorBaseVisitor<Double> {

    @Override
    public Double visitParens(CalculatorParser.ParensContext ctx) {
        return visit(ctx.expr());
    }

    @Override
    public Double visitNumber(CalculatorParser.NumberContext ctx) {
        return Double.valueOf(ctx.NUMBER().getText());
    }

    @Override
    public Double visitMulDiv(CalculatorParser.MulDivContext ctx) {
        Double left = visit(ctx.lhs);
        Double right = visit(ctx.rhs);
        if (ctx.op.getType() == CalculatorParser.MUL) {
            return left * right;
        }
        return left / right;
    }

    @Override
    public Double visitAddSub(CalculatorParser.AddSubContext ctx) {
        Double left = visit(ctx.lhs);
        Double right = visit(ctx.rhs);
        if (ctx.op.getType() == CalculatorParser.ADD) {
            return left + right;
        }
        return left - right;
    }
}